package tt.com.datamanager.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tt.com.common.enums.Status;
import tt.com.common.model.ColdRoom;

import tt.com.common.util.CommonResult;
import tt.com.common.util.ObjectUtil;
import tt.com.common.util.PageUtil;
import tt.com.datamanager.service.ColdRoomService;

import java.util.List;

@RestController
@RequestMapping(value = "coldroom/manager")
public class ColdRoomManagerController {

    @Autowired
    private ColdRoomService coldRoomService;

    @PostMapping(value = "show/page")
    public CommonResult showAllColdRoomByPage(@RequestBody PageUtil pageUtil){
       if (pageUtil.getPageIndex() == null || pageUtil.getPageSize() == null){
           return CommonResult.buildR(Status.PARAM_ISNULL, "参数为空");
       }
        pageUtil = coldRoomService.selectAllColdRoomByPage(pageUtil);
        return CommonResult.buildR(Status.OK, "所有冷藏柜查询成功", pageUtil);
    }


    @PostMapping(value = "update/coldroom")
    public CommonResult updateColdRoom(@RequestBody ColdRoom coldroom){
        if (coldroom == null) {
            return CommonResult.buildR(Status.PARAM_ISNULL, "参数为空");
        }
        if (coldRoomService.updateColdRoom(coldroom, "1") > 0) {
            return CommonResult.buildR(Status.OK, "冷藏柜修改成功");
        } else {
            return CommonResult.buildR(Status.SYSTEM_ERROR, "冷藏柜修改失败");
        }
    }

    @PostMapping(value = "delete/{id}")
    public CommonResult delete(@PathVariable("id") Long id) {
        if (id == null) {
            return CommonResult.buildR(Status.PARAM_ISNULL, "参数为空");
        }
        Integer result = coldRoomService.deleteColdRoom(id,"1");
        if (result == -1) {
            return CommonResult.buildR(Status.SYSTEM_ERROR, "冷藏柜不存在");
        } else if (result == -2) {
            return CommonResult.buildR(Status.SYSTEM_ERROR, "冷藏柜已删除");
        } else {
            return CommonResult.buildR(Status.SYSTEM_ERROR, "冷藏柜删除成功");
        }
    }

    /*@PostMapping(value = "insert")
    public CommonResult insert(@RequestBody List<ColdRoom> list) {
        if (list.size()==0) {
            return CommonResult.buildR(Status.PARAM_ISNULL, "参数为空");
        }
        Integer result = coldRoomService.batchInsert(list);
        if (result == -1) {
            return CommonResult.buildR(Status.SYSTEM_ERROR, "不允许重复添加");
        } else if (result > 0) {
            return CommonResult.buildR(Status.OK, "冷藏柜添加成功");
        } else {
            return CommonResult.buildR(Status.SYSTEM_ERROR, "冷藏柜添加失败");
        }
    }*/

    @PostMapping(value = "insert")
    public CommonResult insert(@RequestBody ColdRoom coldRoom) {
        if (!ObjectUtil.objectIsNull(coldRoom)) {
            return CommonResult.buildR(Status.PARAM_ISNULL, "参数为空");
        }
        Integer result = coldRoomService.insertColdRoom(coldRoom);
        if (result == -1) {
            return CommonResult.buildR(Status.SYSTEM_ERROR, "不允许重复添加");
        } else if (result > 0) {
            return CommonResult.buildR(Status.OK, "冷藏柜添加成功");
        } else {
            return CommonResult.buildR(Status.SYSTEM_ERROR, "冷藏柜添加失败");
        }
    }


}
